package models

import (
	"go-admin-official/modules/database"
)

type PluginsCategoryModel struct {
	Base `json:"-"`

	Id        int64  `json:"id"`
	Name      string `json:"name"`
	Count     int64  `json:"count"`
	CreatedAt string `json:"created_at"`
}

func PluginsCategory() PluginsCategoryModel {
	return PluginsCategoryModel{Base: Base{Table: "plugins_category"}}
}

func (t PluginsCategoryModel) WithTx(tx *database.SqlTx) PluginsCategoryModel {
	t.Tx = tx
	return t
}

func (t PluginsCategoryModel) Find(id interface{}) PluginsCategoryModel {
	item, _ := database.Table(t.Table).Find(id)

	if item == nil {
		return PluginsCategory()
	}

	return t.MapToModel(item)
}

func (t PluginsCategoryModel) Update(key string, value interface{}) (int64, error) {
	return database.Table(t.Table).
		Where("id", "=", t.Id).
		Update(database.H{
			key: value,
		})
}

func (t PluginsCategoryModel) IsEmpty() bool {
	return t.Id == int64(0)
}

func (t PluginsCategoryModel) IsNotEmpty() bool {
	return t.Id != int64(0)
}

func (t PluginsCategoryModel) MapToModel(m map[string]interface{}) PluginsCategoryModel {
	t.Id, _ = m["id"].(int64)
	t.Name, _ = m["name"].(string)
	t.Count, _ = m["count"].(int64)
	t.CreatedAt, _ = m["created_at"].(string)
	return t
}
